package com.example.android.ui.addHB;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.android.R;
import com.example.android.ui.base.BaseActivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AddHBActivity extends BaseActivity implements View.OnClickListener , AddHBContract.IAddHBView {
    private ImageView back;
    private Button commit;
    private Spinner year,month,day,hour,minute,second,type;
    private EditText name,content,reward;
    private AddHBPresenter addHBPresenter;
    @Override
    protected void initViews() {

        back =find(R.id.add_hb_back);
        commit = find(R.id.add_hb_commit);
        name = find(R.id.add_hb_name);
        type = find(R.id.add_hb_type);
        content = find(R.id.add_hb_content);
        reward = find(R.id.add_hb_reward);
        year = find(R.id.add_hb_year);
        month = find(R.id.add_hb_month);
        day =find(R.id.add_hb_day);
        hour = find(R.id.add_hb_hour);
        minute = find(R.id.add_hb_minute);
        second = find(R.id.add_hb_second);

        initSpinner();
        addHBPresenter = new AddHBPresenter(this);
        //监听事件
        back.setOnClickListener(this);
        commit.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_hb;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add_hb_back:
                finish();
                break;
            case R.id.add_hb_commit:
                SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin",0);
                String imagePath = "";
                switch (type.getSelectedItem().toString()){
                    case "快递":
                        imagePath = "https://img-blog.csdnimg.cn/c6a152564d2448c8b5749bf4e05ac564.png";
                        break;
                    case "外卖":
                        imagePath = "https://img-blog.csdnimg.cn/0e174446d4ed4ed3aa1840fe197e2adb.png";
                        break;
                    case "学科竞赛":
                        imagePath = "https://img-blog.csdnimg.cn/27f692096e5f4e7ea0df4e3ed90023d8.png";
                        break;
                }
                int userId = sharedPreferences.getInt("userId",0);
                HashMap<String, Object> map = new HashMap<>();
                map.put("hbReleaseUserid",userId);
                map.put("hbName",name.getText().toString());
                map.put("hbType",type.getSelectedItem().toString());
                map.put("hbIntroduction",content.getText().toString());
                String deadline = year.getSelectedItem().toString()+"-"+month.getSelectedItem().toString()+"-"+day.getSelectedItem().toString()
                        +" "+hour.getSelectedItem().toString()+":"+minute.getSelectedItem().toString()+":"+second.getSelectedItem().toString();
                map.put("hbReleaseDeadlineDatetime",deadline);
                map.put("hbImagePath",imagePath);
                map.put("hbReward", Float.parseFloat(reward.getText().toString()));
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new JSONObject(map).toString());
                addHBPresenter.addHB(requestBody);
                break;
            default:
                break;
        }
    }

    @Override
    public void addHBSuccess(Boolean bool) {
        Toast.makeText(this,"发布成功",Toast.LENGTH_SHORT).show();
        finish();
//        Log.i("AddHBActivity",bool+"");
    }

    @Override
    public void addHBError(Throwable throwable) {
        Toast.makeText(this,"发布失败",Toast.LENGTH_SHORT).show();
//        Log.e("AddHBActivity",throwable.toString());
    }

    public void initSpinner(){
        List<Integer> yearList = new ArrayList<>();
        for (int i=2021;i<2100;i++){
            yearList.add(i);
        }
        ArrayAdapter<Integer> yearAdapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,yearList);
        year.setAdapter(yearAdapter);

        List<Integer> monthList = new ArrayList<>();
        for(int i=1;i<=12;i++){
            monthList.add(i);
        }
        ArrayAdapter<Integer> monthAdapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,monthList);
        month.setAdapter(monthAdapter);

        List<Integer> dayList = new ArrayList<>();
        for(int i=1;i<=31;i++){
            dayList.add(i);
        }
        ArrayAdapter<Integer> dayAdapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,dayList);
        day.setAdapter(dayAdapter);

        List<Integer> hourList = new ArrayList<>();
        for(int i=0;i<=23;i++){
            hourList.add(i);
        }
        ArrayAdapter<Integer> hourAdapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,hourList);
        hour.setAdapter(hourAdapter);

        List<Integer> minuteList = new ArrayList<>();
        for(int i=0;i<=59;i++){
            minuteList.add(i);
        }
        ArrayAdapter<Integer> minuteAdapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,minuteList);
        minute.setAdapter(minuteAdapter);

        List<Integer> secondList = new ArrayList<>();
        for(int i=0;i<=59;i++){
            secondList.add(i);
        }
        ArrayAdapter<Integer> secondAdapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_dropdown_item,android.R.id.text1,secondList);
        second.setAdapter(secondAdapter);
    }
}
